package 面试刷题.小米;

import java.util.Scanner;

/**
 * 这个题给的时间复杂度很宽松
 */
class Solution {
    static int maxn = (int) (1e4 + 10);
    static int maxm = (int) (5e4 + 10);

    /**
     * 0位置：修改的值
     * 1位置时间戳
     * 2类型
     * 3位置
     */
//    static int[][] rows = new int[maxn][3];
//    static int[][] cols = new int[maxn][3];

    /* Write Code Here */
    public long matrixSum(int n, int[][] operations) {
        try {
            int[][] arr = new int[n + 1][n + 1];
            for (int i = 0; i < operations.length; i++) {
                int op = operations[i][0];
                int index = operations[i][1];
                int v = operations[i][2];
                if (op == 0) {
                    for (int j = 0; j < n; j++) {
                        arr[index][j] = v;
                    }
                } else {
                    for (int j = 0; j < n; j++) {
                        arr[j][index] = v;
                    }
                }
            }
            long sum = 0;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    sum += arr[i][j];
                }
            }
            return sum;
        } catch (Exception e) {
            return 0;
        }
    }
}

public class ch03_矩阵元素和 {
    public static void main(String[] args) {
        try {
            Scanner in = new Scanner(System.in);
            long res;

            int n;
            n = Integer.parseInt(in.nextLine().trim());

            int operations_rows = 0;
            int operations_cols = 0;
            operations_rows = in.nextInt();
            operations_cols = in.nextInt();

            int[][] operations = new int[operations_rows][operations_cols];
            for (int operations_i = 0; operations_i < operations_rows; operations_i++) {
                for (int operations_j = 0; operations_j < operations_cols; operations_j++) {
                    operations[operations_i][operations_j] = in.nextInt();
                }
            }

            if (in.hasNextLine()) {
                in.nextLine();
            }


            res = new Solution().matrixSum(n, operations);
            System.out.println(String.valueOf(res));
        } catch (Exception e) {
        }

    }
}
